import os
from pydub import AudioSegment


def get_second_part_wav(main_wav_path, start_time, end_time, part_wav_path):
    """
    音频切片，获取部分音频，单位秒
    :param main_wav_path: 原音频文件路径
    :param start_time: 截取的开始时间
    :param end_time: 截取的结束时间
    :param part_wav_path: 截取后的音频路径
    :return:
    """
    start_time = start_time * 1000  # 因为是毫秒所以需要乘以1000
    end_time = end_time * 1000
    sound = AudioSegment.from_wav(main_wav_path)
    word = sound[start_time:end_time]
    word.export(part_wav_path, format="wav")


if __name__ == '__main__':
    wav_path = "/home/asr_deploy_test/asr_test_file/Test_Ali/Test_Ali_far/audio_dir/R8009_M8028.wav"
    part_wav_path = r'part_wav_path'
    audio = AudioSegment.from_file(wav_path)
    wav_length = int(audio.duration_seconds)
    for i in range(0, wav_length-100, 100):
        new_path = os.path.join(os.path.join(part_wav_path, f"{i}-{i+100}.wav"))
        get_second_part_wav(wav_path, i, i+100, new_path)
    print("Done!")
